<?php

namespace App\Http\Service;

use App\Http\Criteria\GoodsIDCriteria;
use App\Http\Criteria\IsDefaultCriteria;
use App\Http\Criteria\IsActiveCriteria;
use App\Http\Criteria\OrderByCreatedAtCriteria;
use App\Http\Criteria\PaginateCriteria;
use App\Http\Criteria\UserIDCriteria;
use App\Http\Repository\GoodsCommentRepository;


class GoodsCommentService extends BaseService
{
    protected $rep;

    public function __construct(GoodsCommentRepository $repository)
    {
        $this->rep = $repository;

    }

    public function getDefault($goodsId) {

        $this->rep->pushCriteria(new IsDefaultCriteria());

        return $this->list($goodsId);
    }

    public function getList($goodsId, $page) {

        //$this->rep->resetCriteria();
        $this->rep->pushCriteria(new PaginateCriteria($page, 20));
        return $this->list($goodsId);
    }

    public function list($goodsId)
    {
        $this->rep->pushCriteria(new GoodsIDCriteria($goodsId));
        $this->rep->pushCriteria(new IsActiveCriteria());
        $this->rep->pushCriteria(new OrderByCreatedAtCriteria());

        return $this->rep->with(
            ['images' => function($query) {
                $query->orderBy('sort_order', 'desc')->orderBy('created_at', 'desc');
            }])->with(
            ['user' => function($query) {
                $query->select(['id', 'nickname', 'avatar']);
            }])->get();
    }

    public function getMyList($user, $page) {
        $this->rep->pushCriteria(new PaginateCriteria($page, 20));
        $this->rep->pushCriteria(new UserIDCriteria($user->id));
        $this->rep->pushCriteria(new IsActiveCriteria());
        $this->rep->pushCriteria(new OrderByCreatedAtCriteria());

        return $this->rep->with('goods')->with(
            ['images' => function($query) {
                $query->orderBy('sort_order', 'desc')->orderBy('created_at', 'desc');
            }])->with(
            ['user' => function($query) {
                $query->select(['id', 'nickname', 'avatar']);
            }])->get();
    }
}
